package cn.com.greatwall.common.balance.chooser;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.cloud.client.ServiceInstance;

import com.alibaba.nacos.common.utils.CollectionUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author herw
 * @Time 2022-06-20 08:58:30
 * @Version 1.0
 * @Description: 轮询选择器
 */
@Slf4j
public class RoundRuleChooser implements IRuleChooser {
    private AtomicInteger position;
    
    public RoundRuleChooser() {
        this.position = new AtomicInteger(1000);
    }
    
    @Override
    public ServiceInstance choose(List<ServiceInstance> instances) {
        if (CollectionUtils.isNotEmpty(instances)) {
            ServiceInstance serviceInstance = instances.get(Math.abs(position.incrementAndGet() % instances.size()));
            log.info("选择了ip为{}, 端口为：{}的服务", serviceInstance.getHost(), serviceInstance.getPort());;
            return serviceInstance;
        }
        return null;
    }
}
